package com.cpr.demo.config;

import com.cpr.demo.filter.RedisCountFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author Chen Panrui
 * created at 2021/8/31 16:55
 */
@Configuration
@RequiredArgsConstructor
public class FilterConfig {

    private final RedisCountFilter redisCountFilter;

    @Value("${spring.application.name}")
    private String applicationName;

    @Bean
    public FilterRegistrationBean<RedisCountFilter> registrationFilter(@Autowired StringRedisTemplate stringRedisTemplate){
        redisCountFilter.setStringRedisTemplate(stringRedisTemplate);
        redisCountFilter.setApplicationName(applicationName);
        FilterRegistrationBean<RedisCountFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(redisCountFilter);
        registration.addUrlPatterns("/*");
        registration.setName("authFilter");
        registration.setOrder(1);
        return registration;
    }

}
